package com.yxq.mdt.app.modules.video.adapter;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bigkoo.convenientbanner.utils.ScreenUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.video.bean.VideoNearbyListBean;
import com.yxq.mdt.app.utils.DisplayUtil;
import com.yxq.mdt.app.utils.MediaPlayerHelper;

import java.text.DecimalFormat;
import java.util.List;

public class NearbyDataListAdapter extends BaseQuickAdapter<VideoNearbyListBean, BaseViewHolder> {
    public CountDownTimer countDownTimer;
    public boolean isVoiceStart = false;

    public NearbyDataListAdapter(int layoutResId, @Nullable List<VideoNearbyListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, VideoNearbyListBean item) {
        RelativeLayout bgLayout = helper.getView(R.id.open_chat_lint_one);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) bgLayout.getLayoutParams();
        layoutParams.height = (DisplayUtil.getScreenRelatedInformation(mContext) - 3 * ScreenUtil.dip2px(mContext, 15)) / 2;
        bgLayout.setLayoutParams(layoutParams);
        ImageView icon = helper.getView(R.id.item_chat_img);
        GlideHelper.setLoadRoundImage(mContext, item.getImg(), R.mipmap.head_icon, icon);
        ImageView iconPhone = helper.getView(R.id.is_phone);
        LinearLayout voice = helper.getView(R.id.item_number);
        ImageView voiceNote = helper.getView(R.id.item_note);
        TextView voiceTime = helper.getView(R.id.item_number_tv);
        helper.setText(R.id.item_name_tv, item.getName());
        if (item.getNumber() > 1000) {
            helper.setText(R.id.item_number_tv, item.getNumber() / 1000 + "″");//语音文件秒数
        } else {
            helper.setText(R.id.item_number_tv, item.getNumber() + "″");//语音文件秒数
        }
        helper.setText(R.id.item_age_tv, item.getAge() + "岁");
        helper.setText(R.id.item_number_money_tv, item.getMoney());
        helper.setText(R.id.item_number_title_tv, item.getTitle());
        DecimalFormat df = new java.text.DecimalFormat("#.#");
        if (Double.parseDouble(item.getAddressNumber()) >= 1000.00) {
            helper.setText(R.id.item_address_tv, df.format(Double.parseDouble(item.getAddressNumber()) / 1000) + "km");
        } else {
            helper.setText(R.id.item_address_tv, df.format(Double.parseDouble(item.getAddressNumber())) + "m");
        }

        if (item.getAnInt() == 1) {
            iconPhone.setImageResource(R.mipmap.icon_video_telephone);
            helper.setGone(R.id.item_number, true);
            helper.setGone(R.id.item_number_video, false);
        } else {
            iconPhone.setImageResource(R.mipmap.icon_video_video_icon);
            helper.setGone(R.id.item_number, false);
            helper.setGone(R.id.item_number_video, true);
        }
        helper.addOnClickListener(R.id.item_number);
        helper.addOnClickListener(R.id.item_number_video);
        helper.addOnClickListener(R.id.is_phone_layout).addOnClickListener(R.id.open_chat_lint_one);
        voice.setOnClickListener(view -> {
            if (item.getNumber() != 0) {
                if (isVoiceStart) {
                    isVoiceStart = false;
                    MediaPlayerHelper.realese();
                    countDownTimer.onFinish();
                } else {
                    isVoiceStart = true;
                    MediaPlayerHelper.playSound(item.getVicePath(), mediaPlayer -> isVoiceStart = false);
                    if (item.getNumber() > 1000) {
                        getTime(item.getNumber(), voiceNote, voiceTime);
                    } else {
                        getTime(item.getNumber() * 1000, voiceNote, voiceTime);
                    }
                    countDownTimer.start();
                }
            } else {
                voice.setVisibility(View.GONE);
            }
        });
    }

    /**
     * @param playTime  开始时间
     * @param imageView 获取当前控件
     * @param timeTv    获取显示秒数
     */
    private void getTime(int playTime, ImageView imageView, TextView timeTv) {
        countDownTimer = new CountDownTimer(playTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeTv.setText(millisUntilFinished / 1000 + "″");
            }

            @Override
            public void onFinish() {
                timeTv.setText(playTime / 1000 + "″");
                imageView.setImageResource(R.mipmap.icon_video_voice);
                countDownTimer.cancel();
            }
        };
    }

}